package com.cac.demo.controller.packageUnpack;

import com.cac.demo.controller.outStoreRoom.PackageUnpackService;
import com.cac.demo.util.adapter.ResponseMsg;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("packageUnpackRest")
public class PackageUnpackRest {
    @Autowired
    private PackageUnpackService packageUnpackService;

    /**
     * 1.根据IP获取缓存箱列表
     * 2.箱子查询，根据当前工作台IP查ao表，根据aoID查assgin表
     */
    @PostMapping("cacheList")
    public List<HashMap> cacheList(@RequestBody HashMap map){
        HashMap<Object, Object> resultMap = new HashMap<>();
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }

        List<HashMap> hashMaps = packageUnpackService.cacheList(ip);

        return  hashMaps;
    }

    /***
     * 工作包拆包界面，查询拆包的共工作包
     * **/
    @PostMapping("queryPackageTask")
    public PageInfo queryPackageTask(@RequestBody HashMap map){
        HashMap queryMap = (HashMap) map.get("searchParams");
        HashMap page = (HashMap) map.get("page");
        int currentPage = Integer.parseInt(page.get("currentPage").toString());
        int pageSize = Integer.parseInt(page.get("pageSize").toString());
        return packageUnpackService.queryPackageTask(queryMap,currentPage,pageSize);
    }
    /**
     * 查询工作包下的AO
     * **/
    @PostMapping("queryAoByPackage")
    public List<HashMap> queryAoByPackage(@RequestBody HashMap map){
        return packageUnpackService.queryAoByPackage(map);
    }
    /**
     * 根据图号查询对应的工作包
     * **/
    @PostMapping("callUnpackPalletCode")
    public ResponseMsg callUnpackPalletCode(@RequestBody List<HashMap> list){
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        return packageUnpackService.callUnpackPalletCode(list,IP);
    }

    /***
     * 查询托盘下的物料
     * */
    @PostMapping("queryPalletCodeMaterial")
    public ResponseMsg<Object> queryPalletCodeMaterial(@RequestBody HashMap map){
        HashMap page = (HashMap) map.get("page");
        HashMap queryMap = (HashMap) map.get("searchParams");
        int currentPage = Integer.parseInt(page.get("currentPage").toString());
        int pageSize = Integer.parseInt(page.get("pageSize").toString());
        PageInfo pageInfo = packageUnpackService.queryPalletCodeMaterial(currentPage, pageSize, queryMap);
        return new ResponseMsg<>().setResponseBody(pageInfo);
    }

    /***
     * 保存拆包数据
     * */
    @PostMapping("saveUnpackData")
    public ResponseMsg saveUnpackData(@RequestBody HashMap map){
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        String userCode = (String) request.getSession().getAttribute("account");
        if("".equals(userCode)||"guest".equals(userCode)){
            return new ResponseMsg().setRetCode("201").setMessage("当前登录已过期，请重新登录！");
        }
        List<HashMap> list = (List<HashMap>) map.get("matchinfos");
        String workbinCode = (String) map.get("workbinCode");
        String toolCode = (String) map.get("toolCode");
        if("".equals(workbinCode)||"".equals(toolCode)){
            return new ResponseMsg().setRetCode("201").setMessage("未扫箱子或者托盘！");
        }
        return packageUnpackService.saveUnpackData(list,workbinCode,toolCode,userCode,ip);
    }


    /***
     * 查询配送取消需要拆包的数据
     * */
    @PostMapping("querySendCancelData")
    public PageInfo querySendCancelData(@RequestBody HashMap map){
        HashMap queryMap = (HashMap) map.get("queryMap");
        HashMap page = (HashMap) map.get("page");
        int currentPage = Integer.parseInt(page.get("currentPage").toString());
        int pageSize = Integer.parseInt(page.get("pageSize").toString());
        return packageUnpackService.querySendCancelData(queryMap,currentPage,pageSize);
    }
    /***
     * 查询占用信息
     * */
    @PostMapping("queryAllotment")
    public List<HashMap> queryAllotment(@RequestBody HashMap map){
        return packageUnpackService.queryAllotment(map);
    }
    /***
     * 撤销占用，出库
     * */
    @PostMapping("cancelAlloment")
    public ResponseMsg cancelAlloment(@RequestBody HashMap map){
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        String userCode = (String) request.getSession().getAttribute("account");
        if("".equals(userCode)||"guest".equals(userCode)){
            return new ResponseMsg().setRetCode("201").setMessage("当前登录已过期，请重新登录！");
        }
        return packageUnpackService.cancelAlloment(map,ip,userCode);
    }


    /***
     * 撤销配送出库
     * */
    @PostMapping("clearAoMatchInfoSendData")
    public ResponseMsg clearAoMatchInfoSendData(@RequestBody HashMap map){
        return packageUnpackService.clearAoMatchInfoSendData(map);
    }

    /***
     * 撤销配送出库
     * */
    @PostMapping("cancelOut")
    public ResponseMsg cancelOut(@RequestBody HashMap map){
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        String userCode = (String) request.getSession().getAttribute("account");
        if("".equals(userCode)||"guest".equals(userCode)){
            return new ResponseMsg().setRetCode("201").setMessage("当前登录已过期，请重新登录！");
        }
        return packageUnpackService.cancelOut(map,ip,userCode);
    }

    /**
     * 不叫盘拆包
     * **/
    @PostMapping("querySortMaterial")
    public PageInfo querySortMaterial(@RequestBody HashMap map){
        HashMap page = (HashMap) map.get("page");
        HashMap queryMap = (HashMap) map.get("queryMap");
        return packageUnpackService.querySortMaterial(page,queryMap);
    }

    /****
     * 未分拣拆包
     * **/
    @PostMapping("cancelSort")
    public ResponseMsg cancelSort(@RequestBody HashMap map){
        String ip = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            ip = request.getRemoteAddr();
        } else {
            ip = request.getHeader("x-forwarded-for");
        }
        String userCode = (String) request.getSession().getAttribute("account");
        if("".equals(userCode)||"guest".equals(userCode)){
            return new ResponseMsg().setRetCode("201").setMessage("当前登录已过期，请重新登录！");
        }
        return packageUnpackService.cancelSort(map,ip,userCode);
    }
}
